﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008-2009
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.IO

Imports Boogaloo.Core.BLL
Imports Boogaloo.Core.UI
Imports Boogaloo.Admin.UI.Controls

Namespace UI.Webpages


    Partial Public Class WebsiteInformation
        Inherits BaseAdminPage

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            ' special case: attempt to call install script
            If Groove.admin_root Is Nothing Then

                If File.Exists(Server.MapPath("~/Install/Installer.aspx")) Then
                    Response.Redirect("~/Install/Installer.aspx")
                Else
                    Throw New Exception("The '/Install' directory not present and Boogaloo could not retrieve a default 'Root'")
                End If

            Else

                If Not Page.IsPostBack Then
                    BindPage()
                End If

            End If

        End Sub

        Private Sub BindPage()

            ' Website information
            With Groove.admin_root

                If .admin_initial_root Or Root.GetRoots().Count = 1 Then
                    fs_delete.Visible = False
                Else
                    lgd_delete.InnerHtml = String.Format("Delete Website: {0}", .name)
                End If

                lht_name.Text = .name
                lht_name.label = .name
                lht_name.CommandArgument = "name"

                If Not String.IsNullOrEmpty(.description) Then
                    lht_description.label = .description
                End If
                lht_description.Text = .description
                lht_description.CommandArgument = "description"



            End With


            ' Domains
            rpt.DataSource = Groove.admin_root.domains
            rpt.DataBind()

            chk_enforce_primary_domain.Checked = Groove.admin_root.enforce_primary_domain

        End Sub

#Region "Website Information"



        Protected Sub lht_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lht_description.Click, lht_name.Click

            Dim changedTitle As Boolean = False
            Dim lht As LabelHiddenTextbox = CType(sender, LabelHiddenTextbox)

            If Page.IsValid Then

                Dim r As Root = Groove.admin_root

                If lht.CommandArgument = "name" Then
                    If r.name <> lht.Text Then changedTitle = True
                    r.name = lht.Text
                ElseIf lht.CommandArgument = "description" Then
                    r.description = lht.Text
                    changedTitle = True
                End If
                r.Update()


            End If


            If changedTitle Then
                Groove.RefreshPage()
            Else
                BindPage()
            End If

        End Sub



        Protected Sub dwiz_Click(ByVal sender As Object, ByVal e As EventArgs) Handles dwiz.Click

            Root.DeleteRoot(Groove.admin_root.root_id)
            Root.ClearAdminSiteRoot()

            Dim roots As List(Of Root) = Root.GetRoots()
            If roots.Count > 0 Then
                Root.SetAdminInitialRoot(roots(0).root_id)
            End If

            Groove.RefreshPage()
        End Sub

        Protected Sub dwiz_Domain_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim dwiz As CommitWizard = CType(sender, CommitWizard)
            Dim domain_id As Integer = CType(dwiz.CommandArgument, Integer)
            Domain.DeleteDomain(domain_id)
            BindPage()
        End Sub

#End Region


#Region "Domain Names"

        Protected Sub rpt_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt.ItemDataBound
            Dim d As Domain = CType(e.Item.DataItem, Domain)
            If d IsNot Nothing Then

                Dim domain_name As String = d.domain_name
                If d.primary_domain Then domain_name &= " <span>(primary)</span>"
                CType(e.Item.FindControl("lbl_domain_name"), Label).Text = domain_name

                Dim lb_primary As LinkButton = CType(e.Item.FindControl("lb_primary"), LinkButton)
                If d.primary_domain Then
                    lb_primary.Visible = False
                Else
                    lb_primary.CommandArgument = d.domain_id
                End If

                Dim dwiz As CommitWizard = CType(e.Item.FindControl("dwiz"), CommitWizard)
                dwiz.CommandArgument = d.domain_id
                If d.primary_domain Then dwiz.Visible = False

            End If

        End Sub


        Protected Sub lb_add_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_add.Click
            Dim domain_name As String = txt_domainname.Text
            If domain_name.ToLower.StartsWith("http://") Then domain_name = domain_name.Replace("http://", String.Empty)
            Domain.InsertDomain(Groove.admin_root.root_id, txt_domainname.Text)
            txt_domainname.Text = String.Empty


            ' Needed so that ajax refresh does not get in the way of displaying the 
            ' Site map opener link.
            If Groove.admin_root.domains().Count = 1 Then
                Groove.RefreshPage()
            Else
                BindPage()
            End If

        End Sub

        Protected Sub lb_primary_Click(ByVal sender As Object, ByVal e As EventArgs)

            Dim lb As LinkButton = CType(sender, LinkButton)

            Dim domain_id As Integer = CType(lb.CommandArgument, Integer)
            Dim d As Domain = Domain.GetDomain(domain_id)
            If d IsNot Nothing Then
                d.primary_domain = True
                d.Update()
            End If

            BindPage()

        End Sub



        Protected Sub chk_enforce_primary_domain_CheckChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chk_enforce_primary_domain.CheckedChanged

            Dim r As Root = Groove.admin_root
            r.enforce_primary_domain = chk_enforce_primary_domain.Checked
            r.Update()

            saved_message.Visible = True
            BindPage()

        End Sub

#End Region


    End Class

End Namespace